#ifndef COMMON_H
#define COMMON_H

#include <iostream>
#include <string>
#include <vector>

extern "C" {
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/avutil.h>
#include <libavutil/imgutils.h>
#include <libswscale/swscale.h>
#include <libswresample/swresample.h>
}

// 视频信息结构体
struct VideoInfo {
    std::string filename;
    int64_t duration_ms;
    int width;
    int height;
    double fps;
    int bitrate;
    std::string codec_name;
    std::string pixel_format;
    int64_t frame_count;
};

// 错误处理宏
#define CHECK_ERROR(ret, msg) \
    if (ret < 0) { \
        char errbuf[128]; \
        av_strerror(ret, errbuf, sizeof(errbuf)); \
        std::cerr << msg << ": " << errbuf << std::endl; \
        return ret; \
    }

// 初始化FFmpeg
inline void init_ffmpeg() {
    static bool initialized = false;
    if (!initialized) {
        avformat_network_init();
        initialized = true;
    }
}

// 获取视频信息
inline VideoInfo get_video_info(const std::string& filename) {
    VideoInfo info;
    info.filename = filename;
    
    AVFormatContext* fmt_ctx = nullptr;
    int ret = avformat_open_input(&fmt_ctx, filename.c_str(), nullptr, nullptr);
    if (ret < 0) {
        std::cerr << "无法打开文件: " << filename << std::endl;
        return info;
    }
    
    ret = avformat_find_stream_info(fmt_ctx, nullptr);
    if (ret < 0) {
        std::cerr << "无法获取流信息" << std::endl;
        avformat_close_input(&fmt_ctx);
        return info;
    }
    
    int video_stream_index = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0);
    if (video_stream_index < 0) {
        std::cerr << "未找到视频流" << std::endl;
        avformat_close_input(&fmt_ctx);
        return info;
    }
    
    AVStream* video_stream = fmt_ctx->streams[video_stream_index];
    AVCodecParameters* codec_params = video_stream->codecpar;
    
    info.duration_ms = fmt_ctx->duration / 1000;
    info.width = codec_params->width;
    info.height = codec_params->height;
    info.fps = av_q2d(av_guess_frame_rate(fmt_ctx, video_stream, nullptr));
    info.bitrate = codec_params->bit_rate;
    info.frame_count = video_stream->nb_frames;
    
    const AVCodec* codec = avcodec_find_decoder(codec_params->codec_id);
    if (codec) {
        info.codec_name = codec->name;
    }
    
    const char* pix_fmt_name = av_get_pix_fmt_name((AVPixelFormat)codec_params->format);
    if (pix_fmt_name) {
        info.pixel_format = pix_fmt_name;
    }
    
    avformat_close_input(&fmt_ctx);
    return info;
}

// 打印视频信息
inline void print_video_info(const VideoInfo& info) {
    std::cout << "📹 视频信息: " << info.filename << std::endl;
    std::cout << "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" << std::endl;
    std::cout << "⏱️  时长: " << (info.duration_ms / 1000) << "." 
              << std::setw(3) << std::setfill('0') << (info.duration_ms % 1000) << " 秒" << std::endl;
    std::cout << "📐 分辨率: " << info.width << " × " << info.height << std::endl;
    std::cout << "🎞️  帧率: " << std::fixed << std::setprecision(2) << info.fps << " fps" << std::endl;
    std::cout << "📊 码率: " << (info.bitrate / 1000) << " kbps" << std::endl;
    std::cout << "🎬 编码格式: " << info.codec_name << std::endl;
    std::cout << "🎨 像素格式: " << info.pixel_format << std::endl;
    std::cout << "🔢 总帧数: " << info.frame_count << std::endl;
    std::cout << "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" << std::endl;
}

#endif // COMMON_H
